//
//  OMGGongLueCollectionViewCell.m
//  A_project2.0
//
//  Created by dllo on 16/3/15.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "OMGGongLueCollectionViewCell.h"
#import "OMGGongLueTableViewCell.h"
#import "OMGGongLueModel.h"

#import <AFNetworking.h>
#import <UIImageView+AFNetworking.h>
#import <MJRefresh.h>

#import "QPNetWorkingTool.h"

@interface OMGGongLueCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger _page;
    
}
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;

@end


@implementation OMGGongLueCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArr = [NSMutableArray array];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, self.contentView.frame.size.width, 557) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        // 滚动条隐藏
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        
        [self.contentView addSubview:_tableView];
        // 重用池
        [_tableView registerClass:[OMGGongLueTableViewCell class] forCellReuseIdentifier:@"cell"];
        
        // 调用data数据
        _page = 1;
        //        [self data];
        // 初始化
        
        // 刷新加载
        [self refreshUI];
        
    }
    return self;
    
}
- (void)data
{
    
    NSString *str1 = @"http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&delcache=1&forum_id=替换&forum_type=1%2C2%2C6&lat=38.88273365799082&lon=121.5392219061215&oauth_token=fd676729155d47e8855e62142686c1be&page=我的";
    NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"替换" withString:_str];
    NSString *str3 = [str2 stringByReplacingOccurrencesOfString:@"我的" withString:[NSString stringWithFormat:@"%ld", _page]];
    NSLog(@"%@", str3);
    
    NSString *url = @"&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=B9CA7896-B35E-4798-A060-4DC4EC2BDCD2&track_os=ios%25208.4.1&track_user_id=6928936&v=1";
    
    
    [QPNetWorkingTool getNetWorkingTool:[NSString stringWithFormat:@"%@%@", str3, url] dic:nil success:^(id result) {
        
        NSArray *arr = result[@"data"][@"entry"];
        for (NSDictionary *dic in arr) {
            OMGGongLueModel *m = [OMGGongLueModel modelWithDic:dic];
            [self.dataArr addObject:m];
            
            
        }
        [_tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

    } fail:^(NSError *error) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMGGongLueModel *m = _dataArr[indexPath.row];
    OMGGongLueTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.titleLabel.text = m.username;
    [cell.img setImageWithURL:[NSURL URLWithString:m.avatar]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    cell.xiangqingLabel.text = m.title;
    return cell;
    
}

#pragma mark - cell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMGGongLueModel *m = _dataArr[indexPath.row];
    [self.delegate gongLueWebPush:m.view_url];
    
}

- (void)refreshUI
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self data];
    }];
    self.tableView.mj_header = header;
    [header setTitle:@"loading" forState:MJRefreshStateRefreshing];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _page++;
        //加载数据
        [self data];
        
    }];
    
    
}

-(void)setDataArr:(NSMutableArray *)dataArr{
    
    if (_dataArr != dataArr) {
        _dataArr = dataArr;
    }
    [self.tableView reloadData];
}


@end
